package cn.tang.tframe.sdm.web.controller;

import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 自定义记录controller请求响应日志，
 * 
 * @author tanghc
 */
// 会被Spring-MVC自动扫描，但又不属于Controller的annotation。
@ControllerAdvice
@Order(1)
public class ControllerResponseLoggerHandler implements ResponseBodyAdvice<Object> {
//	private Logger logger = LoggerUtil.accessLog;

	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		return true;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
//		if (request instanceof ServletServerHttpRequest) {
//			logger.debug("after controller commrequest: body={},requestUri={},baseparam={},requestParam={}", body,
//					request.getURI(), RequestContextAttributeHolder.getRequestAttributes(),
//					ServletUtil.dumpRequestParam(((ServletServerHttpRequest) request).getServletRequest()));
//		} else {
//			logger.debug("after controller mulrequest: body={},requestUri={},baseparam={}", body, request.getURI(),
//					RequestContextAttributeHolder.getRequestAttributes());
//		}
		return body;
	}
}
